#pragma once

#include <iostream>
#include <unordered_map>
#include <string>
#include <functional>
#include <ctime>
#include <sys/types.h>
#include <unistd.h>

using std::cout;
using std::endl;
using task_t = std::function<void()>;

void DownLoad()
{
    cout << "我是一个下载任务" << endl;
}
void Log()
{
    cout << "我是一个日志任务" << endl;
}
void Sql()
{
    cout << "我是一个数据同步任务" << endl;
}

static int number = 0;
class TaskManger
{
public:
    TaskManger()
    {
        srand(time(nullptr));
        InserTask(DownLoad);
        InserTask(Log);
        InserTask(Sql);
    }
    void InserTask(task_t t)
    {
        tasks[number++] = t;
    }
    void Execute(int number)
    {
        if (tasks.find(number) == tasks.end())
        {
            return;
        }
        tasks[number]();
    }

    int Selectask()
    {
        return rand() % number;
    }

    ~TaskManger()
    {
    }

private:
    std::unordered_map<int, task_t> tasks;
};

TaskManger ttm;

void Worker()
{
    int cmd = 0;
    while (true)
    {
        int n = read(0, &cmd, sizeof(cmd));
        if (n == sizeof(cmd))
        {
            ttm.Execute(cmd);
        }
        else if (n == 0)
        {
            std::cout << getpid() << ", child close history fd: " << endl;
            break;
        }
        else
        {
        }
    }
}